package com.baizhi.service.ServiceImpl;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.dao.BannerDao;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/6.
 */
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerDao bannerDao;

    @Override
    @LogAnnotation("查询所有轮播图")
    public Map selectAllBanner(int rows,int page,String bannerName) {
        Map map=new HashMap();
        if(bannerName!=null){
            if("".equals(bannerName)){
                bannerName=null;
            }else{
                bannerName="%"+bannerName+"%";
            }
        }
        int begin=rows*(page-1);
        int size=rows;
        Integer count=bannerDao.selectAllBannerCount(bannerName);
        List<Banner> users=bannerDao.selectAllBanner( begin,size,bannerName);

        map.put("total",count);
        map.put("rows",users);
        return map;
    }

    @Override
    public Map addBanner(Banner banner) {
        Map map=new HashMap();
        try {
            bannerDao.addBanner(banner);
            map.put("isok",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isok",false);
        }
        return  map;
    }

    @Override
    public Map delatebanner(Integer[] ids) {
        Map map=new HashMap();
        try {
            bannerDao.delatebanner(ids);
            map.put("isok",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isok",false);
        }
        return  map;
    }

    @Override
    public Map updateBanner(Banner banner) {
        Map map=new HashMap();
        try {
            bannerDao.updateBanner(banner);
            map.put("isok",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isok",false);
        }
        return  map;
    }
}
